import React from 'react';
import { inject, observer } from 'mobx-react';
import {WeaTableNew} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;

import { WeaErrorPage, WeaTools } from 'ecCom';
import {WeaTop,WeaTab,WeaRightMenu} from 'ecCom';
import {Input,Button,Alert,Spin,Icon,Modal,message} from 'antd';
import {Condition} from './ListCondition';
const confirm = Modal.confirm;
import isEmpty from 'lodash/isEmpty'
import forEach from 'lodash/forEach'
import {routerShape} from 'react-router';
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import FnaPermission from '../public/FnaPermission';
import * as openUrl from '../util/pulic-func';
import * as API_HIS from '../apis/fnaReportBase'

/**
 * 预付款查询表
 */
@inject('fnaAdvancePaymentStore')
@observer
class FnaAdvancePayment extends React.Component {
	static contextTypes = {
	    router: routerShape
	};

	constructor(props) {
		super(props);
	}

	componentDidMount() {
		//一些初始化请求
		const { fnaAdvancePaymentStore } = this.props;	
		const {initDatas,setRouter} = fnaAdvancePaymentStore;
		
		let {router} = this.context;
        setRouter(router);	
		initDatas();

	}
	
	componentWillReceiveProps(nextProps) {
		
	}
	shouldComponentUpdate(nextProps, nextState) {
		
		return true;
	}
	componentWillUnmount() {
		//组件卸载时一般清理一些状态

	}

	render() {
		const {fnaAdvancePaymentStore} = this.props;
		const {hasRight,loading,showSearchAd,topTab,tableStore} = fnaAdvancePaymentStore;

		return (
			<div style={{height:'100%'}}>
                {hasRight == 1 && <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@dk5w4r`} datas={this.getRightMenu()} collectParams={{favname:getLabel(28111,'收藏'), favouritetype:5}} >
					<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@9fz11g`}
						title={getLabel(390432,'预付款查询表')}
						loading={loading}
						icon={<i className='icon-coms-Finance'/>}
						iconBgcolor='#f14a2d'
						buttons={this.getTopButton()}
						buttonSpace={10}
						showDropIcon={true}
						dropMenuDatas={this.getRightMenu()}
					>
					</WeaTop>

					<WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@5r0f1a`}
						buttons={[]}
						buttonsAd={this.getAdButtons()}
						searchType={'advanced'}
						searchsBaseValue={''}//默认值
						setShowSearchAd={this.setShowSearchAd}
						hideSearchAd={this.doHideSearchAd}
						searchsAd={
							<Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@phi8dr`} isEcharform={false} listStore={fnaAdvancePaymentStore} onEnterSearch={this.enterSearch}/>
						}
						showSearchAd={showSearchAd}
						datas={topTab}
						keyParam="key"  //主键
						advanceHeight={250}
					/>

				<WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@eiy19u`} 
                    comsWeaTableStore={tableStore}
                	hasOrder={true}
                	needScroll={true} 
                />					
					
				</WeaRightMenu>}
				{hasRight == 4 && <FnaPermission ecId={`${this && this.props && this.props.ecId || ''}_FnaPermission@z5ha1l`}/>}
			</div>
		)
	}

	doHideSearchAd = () => {
		this.props.fnaAdvancePaymentStore.setShowSearchAd(false);
    }
	
	setShowSearchAd = (bool) => {
		this.props.fnaAdvancePaymentStore.setShowSearchAd(bool);
	}

	getRightMenu=()=>{
		const { fnaAdvancePaymentStore } = this.props;
		const {tableStore,dataKey,isSearch} = fnaAdvancePaymentStore;
		let btns = [];

		if(isSearch){
			btns.push({
				key:"excelExport",
				icon: <i className="icon-coms-download2" />,
				content:getLabel(28343,'导出Excel'),
				onClick: this.exportExcel.bind(this)
			});
		}

		btns.push({
            key:"colSet",
    		icon: <i className='icon-coms-Custom'/>,
            content:getLabel(32535,'显示列定制'),
            onClick: () =>{
                tableStore.setColSetVisible(dataKey,true);
    		    tableStore.tableColSet(dataKey,true);
            }
    	});
		return btns;
	}

	//tab右侧按钮菜单
	getTopButton=()=>{
		const { fnaAdvancePaymentStore } = this.props;
		const { isSearch } = fnaAdvancePaymentStore;
		let btns=[];

		//只要点击了搜索都会出现导出Excel
		if(isSearch){
			btns.push((<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@a50r1v@ExportExcel`} type="primary" onClick={this.exportExcel.bind(this)}>{getLabel(28343,'导出Excel')}</Button>));
		}
		return btns;
	}

	//导出Excel
    exportExcel(){
   	    const { fnaAdvancePaymentStore } = this.props;
   	    const { guid } = fnaAdvancePaymentStore;
    	let url = "/api/fna/excel/exportAdvance?_guid1=" + guid;
    	window.location.href = url;
    }	
	
	//获取高级搜索的form底部的按钮
    getAdButtons=() =>{
    	const {fnaAdvancePaymentStore} = this.props;
    	const {doSearch,setShowSearchAd,clearFormFields,generGUID} = fnaAdvancePaymentStore;
		let buts;
		buts = [
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@88lpkb@Search`} type="primary" onClick={()=>{generGUID(this.showConfirm.bind(this));}}>{getLabel(197,'搜索')}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ceq9it@Reset`} type="ghost" onClick={()=>{clearFormFields(0);}}>{getLabel(2022,'重置')}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@emhbpg@Cancel`} type="ghost" onClick={()=>{setShowSearchAd(false)}}>{getLabel(31129,'取消')}</Button>)
		];

        return buts;
	}	
	
	/**
	* 高级搜索回车搜索
	*/
	enterSearch = () => {
		const {fnaAdvancePaymentStore} = this.props;
    	const {generGUID} = fnaAdvancePaymentStore;
		generGUID(this.showConfirm.bind(this));
	}

	
	showConfirm=()=>{
		let that  = this;
		const {fnaAdvancePaymentStore} = this.props;
		const {setShowSearchAd,setIsSearch} = fnaAdvancePaymentStore;
		confirm({
			className:'fnaConfirm',
		    title: getLabel(131329,'信息确认'),
		    content: getLabel(82522,'确定要生成报表么?'),
		    okText: getLabel(826,'确定'),
			cancelText: getLabel(31129,'取消'),
		    onOk() {
				setIsSearch(true);//点击了高级搜索
				that.doSearch();
				setShowSearchAd(false);//高级搜索隐藏
		    
		    },
		    onCancel() {

		    },
		});
	}

	//获取表单数据查询
    doSearch=()=>{
		const {fnaAdvancePaymentStore} = this.props;
		const {guid,form,saveTempData} = fnaAdvancePaymentStore;
		const searchParamsAd = form.getFormParams();
		searchParamsAd.rptViewType = 0;
		searchParamsAd.qryFunctionType = 1;
		searchParamsAd._guid1 = guid;

		//这里存在WeaTool的异步请求,所以只能在保存的action里面回调进行查询
    	saveTempData(searchParamsAd,()=>{
			this.getFnaLoading(searchParamsAd);
		});

    	//调用查询线程进度方法
    	// const hide = message.loading(getLabel(385366,'正在执行中...'), 0);
		// this.getFnaLoading(searchParamsAd,hide);
	}
	
    getFnaLoading=(params,hide)=>{
		const {fnaAdvancePaymentStore} = this.props;
		const { guid,doSearch,form } = fnaAdvancePaymentStore;
		const searchParamsAd = form.getFormParams();
		const newParams = { ...searchParamsAd, ...params };
		doSearch(newParams);

		// API_HIS.getFnaLoadingInfo({'guid':guid}).then(data =>{
		// 	if(data.flag){
		// 		// 异步手动移除
  		// 		setTimeout(hide,1000);
  		// 		if(data.result.flag){
		// 			doSearch(newParams)
  		// 		}else{
  		// 			alert(data.result.msg);
  		// 		}				
		// 	}else{
		// 		let hideNew = hide;					
		// 		setTimeout(this.getFnaLoading(params,hideNew),2000); 
		// 	}

		// });	

	}	

	

}
export default WeaTools.tryCatch(React,
	props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@ugwoj9`} msg={ props.error ? props.error : getLabel(385218,'对不起,该页面异常,请联系管理员!')} />,
	{error: ""}
)(FnaAdvancePayment);




